<template>
    <view class="page">
        <u-navbar title="首页" :placeholder="true" leftIcon="">
        </u-navbar>
        <view class="BigBox">
            <view class="headerBox">
                <view class="img">
                    <view class="search" @click="search(placeholderText)">
                        <u-input class="input" disabled prefixIcon="search" border="none"
                            :placeholder="placeholderText">
                        </u-input>
                        <u-button class="button">搜索</u-button>
                    </view>
                    <view class="swiper">
                        <u-swiper class="slider" :list="bannerList" indicator indicatorMode="line" circular
                            radius="1rem" height="10rem"></u-swiper>
                    </view>
                </view>
            </view>
            <view class="subBar">
                <view class="hot " @click="handleHot">
                    <view class="hotSearch"></view>
                    <span>热门榜单</span>
                </view>
                <view class="classifyBox " @click="handleClass">
                    <view class="classify"></view>
                    <span>书籍分类</span>
                </view>
            </view>
        </view>
        <view class="newest">
            <view class="newestTitle">
                <view class="newestTitleLeft">
                    <span>最新上架</span>
                </view>
                <!-- <view class="newestTitleRight">
                    <u-icon name="more-dot-fill" size="25px"></u-icon>
                </view> -->
            </view>
            <view class="newestList" v-if="newBookList.length > 0">

                <bookCover v-for="item in newBookList" :key="item.id" @click.native="goDetail(item)" class="bookCover"
                    :book="item" />
            </view>
            <view class="divider">
                <u-divider text="人家也是有底线的啦~"></u-divider>
            </view>
        </view>
        <view class="footer">
            <TabBar :tabIndex="1" :messageNum="9"></TabBar>
        </view>
    </view>
</template>

<script>
import _ from 'lodash'
import TabBar from '@/components/tabBar.vue'
import bookCover from '@/components/bookCover.vue'
import { mapGetters } from 'vuex'
export default {
    components: {
        TabBar,
        bookCover
    },
    computed: {
        ...mapGetters(['newBookList'])
    },
    onLoad() {
        this.getNewBookList(this.page)
    },
    data() {
        return {
            page: 10,//默认10条，随着用户下来，进行刷新加载更多条数
            placeholderText: 'Search for book',
            bannerList: [
                "https://cdn.uviewui.com/uview/swiper/swiper1.png",
                "https://cdn.uviewui.com/uview/swiper/swiper2.png",
                "https://cdn.uviewui.com/uview/swiper/swiper3.png",
            ],
        };
    },
    methods: {
        getNewBookList(page) {
            this.$store.dispatch('getNewBookList', page)
        },
        goDetail(item) {
            //已经拿到书籍的全部信息，直接跳转到详情页
            this.$store.dispatch('getBookDetail', item.id).then(res => {
                if (res == 'success') {
                    this.$Router.push({
                        name: 'detail',
                    })
                } else {
                    this.$tools.toast('请求失败，请稍后再试')
                }
            })

        },
        search(placeholderText) {
            this.$store.dispatch('getHotBookList')
            this.$Router.push({
                name: 'search',
                query: {
                    placeholderText
                }
            })
        },

        handleHot() {
            // 在点击热门按钮的时候，获取热门书籍
            this.$store.dispatch('getHotBookList')
            this.$Router.push({
                name: 'hotList'
            })

        },
        handleClass() {
            this.$store.dispatch('GetClass')
            this.$Router.push({
                name: 'classify'
            })

        }
    }
}
</script>

<style lang="scss">
page {
    height: 90%;

    .page {
        height: 100%;
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;

        .BigBox {
            width: 95%;

            .headerBox {
                margin-top: 1rem;
                width: 100%;
                height: 20rem;

                .img {
                    width: 100%;
                    height: 70%;
                    border-radius: 2rem;
                    background-image: url('@/static/images/home_header.png');
                    background-size: cover;
                    background-position: center;
                    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.2);
                    display: flex;
                    flex-direction: column;
                    align-items: center;

                    .search {
                        margin-top: 3rem;
                        display: flex;
                        flex-direction: row;
                        justify-content: space-between;
                        align-items: center;
                        box-shadow: 0 0.4rem 0.8rem rgba(0, 0, 0, 0.2);
                        height: 3rem;
                        border-radius: 1rem;
                        background: rgba(255, 255, 255, 0.8);
                        width: 90%;

                        .input {
                            margin: 0 1rem;
                            width: 80%;
                        }

                        .button {
                            background: transparent;
                            border-radius: 0;
                            border: none;
                            border-left: 1px solid #ccc;
                            width: 4rem;
                        }
                    }

                    .swiper {
                        width: 100%;
                        display: flex;
                        flex-direction: row;
                        justify-content: center;
                        margin-top: 2rem;

                        .slider {
                            box-shadow: 0 0.4rem 0.8rem rgba(0, 0, 0, 0.2);
                            width: 95%;
                        }
                    }
                }
            }

            .subBar {
                width: 100%;
                height: 12rem;
                display: flex;
                flex-direction: row;
                justify-content: space-around;
                // 字体阴影

                .hot {
                    background: $uni-color-theme-3;
                    width: 40%;
                    height: 80%;
                    display: flex;
                    flex-direction: column;
                    justify-content: space-around;
                    align-items: center;
                    border-radius: 2rem;
                    margin-left: 0.5rem;
                    box-shadow: 0 0.2rem 0.4rem rgba(0, 0, 0, 0.2);

                    .hotSearch {
                        width: 100%;
                        height: 100%;
                        background-image: url('@/static/images/hotSearch.png');
                        background-size: 100% 100%;
                        background-position: center;
                    }

                    span {
                        display: inline-block;
                        margin-bottom: 0.5rem;
                        font-size: 1.2rem;
                        color: #fff;
                        // 字体阴影
                        text-shadow: 0 0.1rem 0.2rem rgba(0, 0, 0, 0.8);

                    }
                }

                .classifyBox {
                    margin-left: 0.5rem;
                    border-radius: 2rem;
                    background: $uni-color-theme-1;
                    width: 40%;
                    height: 80%;
                    display: flex;
                    flex-direction: column;
                    justify-content: space-around;
                    align-items: center;
                    box-shadow: 0 0.2rem 0.4rem rgba(0, 0, 0, 0.2);

                    .classify {
                        width: 100%;
                        height: 100%;
                        background-image: url('@/static/images/classify.png');
                        background-size: 100% 100%;
                        background-position: center;
                    }

                    span {
                        display: inline-block;
                        margin-bottom: 0.5rem;
                        font-size: 1.2rem;
                        color: #fff;
                        text-shadow: 0 0.1rem 0.2rem rgba(0, 0, 0, 0.8);
                    }


                }

            }
        }

        .newest {
            width: 90%;

            .newestTitle {
                display: flex;
                flex-direction: row;
                justify-content: space-between;
                width: 100%;
            }

            .divider {
                width: 100%;
                height: 6rem;
            }

            .newestList {
                display: flex;
                flex-direction: row;
                justify-content: space-around;
                flex-wrap: wrap;
                align-items: center;
                // margin-top: 1rem;
                // margin-bottom: 1rem;

                .bookCover {
                    width: 100%;

                    // height: 7rem;
                    margin: 0.5rem;
                }

            }

        }

        .footer {
            margin-top: 3rem;
            width: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;

            .text {
                display: block;
                color: #ccc;
                font-size: 0.8rem;
                margin-bottom: 5rem;
            }
        }
    }

}
</style>